package DensityObjects
{
	import FeedbackObjects.MessageBox;
	import MyDisplayObjects.MyButton;
	import flash.display.MovieClip;
	import flash.display.Shape;
	import flash.events.Event;
	import flash.events.MouseEvent;
	
	public class DroppingPanel extends MovieClip
	{
		public static const BACKGROUND_COLOR:uint = 0xFFFFDD;
		
		private var _beaker:Beaker;
		private var _densityButton:MyButton;
		private var _allowShowDensity:Boolean;
		private var _showingDensity:Boolean = false;
		
		public function DroppingPanel(width:Number, height:Number, allowShowDensity:Boolean = true)
		{
			this.graphics.beginFill(BACKGROUND_COLOR,1);
			this.graphics.drawRect(0, 0, width, height);
			this.graphics.endFill();
			_allowShowDensity = allowShowDensity;
			this.addEventListener(Event.ADDED, handleAdded);
			
		}
		private function handleAdded(event:Event):void
		{
			this.removeEventListener(Event.ADDED, handleAdded);
			
			if (_allowShowDensity)
			{
				_densityButton = new MyButton (this.width/4, this.width/8, this.width/16, this.width/16, "Show densities", 0xDDDDFF, 0, true, false, MyButton.AUTO_COLOR, "", MyButton.AUTO_COLOR, MyButton.AUTO_COLOR, "", MyButton.AUTO_COLOR, MyButton.AUTO_COLOR, "Hide densities");
				_densityButton.addEventListener(MouseEvent.CLICK, handleClickDensityButton);
				_densityButton.x = this.width - _densityButton.width;
				_densityButton.y = this.height - _densityButton.height;
				this.addChild(_densityButton);
			}
			
			_beaker = new Beaker (100, 5, 70);
			this.addChild (_beaker);
			_beaker.x = (this.width - _beaker.width)/2; _beaker.y = this.height - _beaker.height;
			// put a ground underneath the beaker
			var ground:Shape = new Shape();
			ground.graphics.beginFill(FlickingPanel.GROUND_COLOR, 1);
			ground.graphics.drawRect(0, this.height-_beaker.pxCircHeight, this.width, _beaker.pxCircHeight);
			ground.graphics.endFill();
			this.addChildAt(ground, 0);
			
		}
		
		/** If user clicks on density button go through each flicable mc and show density */
		private function handleClickDensityButton (event:MouseEvent):void
		{
			
			var fmc:FlickableMC;
			if (_showingDensity)
			{
				_beaker.hideDensity();
				_showingDensity = false;
			} else
			{
				_beaker.showDensity();
				_showingDensity = true;
			}	
		}
		
		
		/** This function is used to add a new flickable object (or shape) to the panel, at specified location */
		public function addDroppableMC (type:String, mass:Number, volume:Number=0)
		{
			_beaker.addDroppableMC(type, mass, volume);
		}
		
		public function addNewMessage (_message:String, initx:Number, inity:Number, type:String, id:String="", MAX_WIDTH:Number=Number.MAX_VALUE, MAX_HEIGHT:Number=Number.MAX_VALUE)
		{
			var m:MessageBox = new MessageBox(_message, initx, inity, type, id, MAX_WIDTH, MAX_HEIGHT);
			this.addChild(m);			
		}
	}
}